<template>
  <div class="app-container">
    <el-card class="operate-container" shadow="never">
      <el-form :inline="true">
        <el-form-item label="您所需要查看的订单信息！">
        </el-form-item>
      </el-form>
    </el-card>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="序号" width="70">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="流水号" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.sn }}
        </template>
      </el-table-column>
      <el-table-column label="门店名称" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.shopName }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="门店商品" width="200" align="center">
        <template slot-scope="scope">
          <div v-for="(item,index) in scope.row.shopItem">
            {{ item }}
            <el-tag type="warning">x{{ scope.row.itemNum[index] }}</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="商品单价" width="200" align="center">
        <template slot-scope="scope">
          <div v-for="item in scope.row.itemPrice">
            ￥{{ item }}
          </div>
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="总价格" width="200" align="center">
        <template slot-scope="scope">
          ￥{{ parseFloat(scope.row.totalPrice).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="订单状态" width="200" align="center"
                       :selectable="checkSelectable"
      >
        <template slot-scope="scope">
          <el-tag type="danger" v-show="scope.row.status === '未支付'">{{ scope.row.status }}</el-tag>
          <el-tag type="success" v-show="scope.row.status === '已支付'">{{ scope.row.status }}</el-tag>
          <el-tag type="info" v-show="scope.row.status === '已退款'">{{ scope.row.status }}</el-tag>
          <el-tag v-show="scope.row.status === '已确认'">{{ scope.row.status }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="auto" align="center" >
        <template slot-scope="scope">
          <el-button style="padding: 5px" type="text" v-show="flag === 'customer'"
                     @click="removeOrder(scope.row.sn)"
          >取消
          </el-button>
          <el-button type="text" v-show="flag === 'customer'" @click="paymentOrder(scope.row.sn)">支付</el-button>
          <el-button type="text" v-show="flag === 'business'" @click="returnAmountOrder(scope.row.sn)">退款</el-button>
          <el-button type="text" v-show="flag === 'business'" @click="enterOrder(scope.row.sn)">确认</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="size"
      :page-sizes="[5, 10, 15, 20, 35, 40, 100]"
      style="padding: 30px 0; text-align: center;"
      layout="sizes, prev, pager, next, jumper, ->, total, slot"
      @current-change="fetchData"
      @size-change="changeSize"
    />
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import { pageOrder, removeOrder, paymentOrder, enterOrder, returnAmountOrder } from '@/api/order'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      shop: {},
      page: 1,
      size: 5,
      total: 0,
      srcList: [
        'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
        'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg'
      ],
      flag: '',
      dialogFormVisible: false,
      orderOjb: {
        sn: '',
        shopName: '',
        shopItem: [],
        itemNum: [],
        itemPrice: [],
        totalPrice: '',
        state: ''
      }
    }
  },
  created() {
    this.viewByTokenFlag()
    this.fetchData()
  },
  methods: {
    // 当页码发生改变的时候
    changeSize(size) {
      this.size = size
      this.fetchData(1)
    },
    // 查询门店
    fetchData(page = 1) {
      this.page = page
      this.listLoading = true
      let token = getToken()
      let userId = JSON.parse(token).id
      pageOrder(this.page, this.size, userId).then(response => {
        const records = response.data.records
        console.log(records)
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    // 根据角色展示不同视图
    viewByTokenFlag() {
      let token = getToken()
      let parseToken = JSON.parse(token)
      this.flag = parseToken.flag
    },
    removeOrder(sn) {
      removeOrder(sn).then(response => {
        this.$message({
          showClose: true,
          message: '订单取消成功！',
          type: 'success'
        })
        this.fetchData(1)
      }).catch(() => {
      })
    },
    paymentOrder(sn) {
      paymentOrder(sn).then(response => {
        this.$message({
          showClose: true,
          message: '订单支付成功！',
          type: 'success'
        })
        this.fetchData(1)
      }).catch(() => {
      })
    },
    returnAmountOrder(sn) {
      returnAmountOrder(sn).then(response => {
        this.$message({
          showClose: true,
          message: '订单退款成功！！',
          type: 'success'
        })
        this.fetchData(1)
      }).catch(() => {

      })
    },
    enterOrder(sn) {
      enterOrder(sn).then(response => {
        this.$message({
          showClose: true,
          message: '订单确认成功！！',
          type: 'success'
        })
        this.fetchData(1)
      }).catch(() => {

      })
    },
    checkSelectable(row) {
      return row.status !== '已支付'
    }
  }
}
</script>
<style>
.time {
  font-size: 13px;
  color: #999;
}

.bottom {
  margin-top: 13px;
  line-height: 12px;
}

.button {
  padding: 0;
  float: right;
}

.image {
  width: 100%;
  display: block;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}
</style>
